package com.dayatang.android.Activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ExecutionException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.ListView;

import com.dayatang.android.Adapter.SubProjectListViewAdapter;
import com.dayatang.android.Model.SubProject;
import com.dayatang.android.Task.SubProjectListTask;

public class SubProjectListActivity extends FootCommonActivity {
	
	private ListView listView;
    private SubProjectListViewAdapter simpleAdapter;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		AlabSetContentView(R.layout.subproject_list_activity);
		InitListView();
		AlabSetBarTitleText(getCurProject().getName());
	}
	
	private void InitListView() {
		
		listView = (ListView) findViewById(R.id.subproject_list_listview);
		try {
			Log.i("123", getCurProject().getProjectId() + "===");
			String result = new SubProjectListTask(this).execute(getServerAddress(),getCurProject().getProjectId(),getUser().getUsername()).get();
			if (result.equals("error")) {
				ToastShow(getString(R.string.serverAddress_alert_addressError));
			} else if (result.equals("NoData")) {
				ToastShow(getString(R.string.subproject_no_subproject));
			} else if(result != null) {
				simpleAdapter = new SubProjectListViewAdapter(this, ProcessData(result));
				listView.setAdapter(simpleAdapter);
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
		
	}
	
	private List<HashMap<String, Object>> ProcessData(String data) {
		
		List<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();
		projectSearchServiceDb.delAllSubProject();
		
		try {
			JSONArray jsonArray = new JSONArray(data);
			for(int i = 0; i<jsonArray.length(); i++) {  
				JSONObject jsonObject = (JSONObject) jsonArray.get(i);  
				
				HashMap<String, Object> map = new HashMap<String, Object>();
			    map.put("ItemProjectName",jsonObject.getString("projName"));
			    map.put("ItemProjectState",jsonObject.getString("output")+getString(R.string.subproject_finish_output_unit));
			    map.put("ItemProjectId", jsonObject.getString("id"));
				map.put("ItemProjectBudget", jsonObject.getString("budget")+getString(R.string.subproject_finish_output_unit));
			    
			    SubProject project = new SubProject();
			    project.setSubprojectid(jsonObject.getString("id"));
			    project.setName(jsonObject.getString("projName"));
			    project.setStartDate(jsonObject.getString("startDate"));
			    project.setFinishDate(jsonObject.getString("finishDate"));
			    project.setProjectType(jsonObject.getString("projectType"));
			    project.setArea(jsonObject.getString("area"));
			    project.setResponsibleDivision(jsonObject.getString("responsibleDivision"));
			    project.setBudget(jsonObject.getString("budget"));
			    project.setOutput(jsonObject.getString("output"));
			    project.setOwnerInfo(jsonObject.getString("ownerInfo"));
			    project.setSupervisorInfo(jsonObject.getString("supervisorInfo"));
			    project.setDesignerInfo(jsonObject.getString("designerInfo"));
			   	project.setProjectid(getCurProject().getProjectId());
			   	project.setApType(jsonObject.getString("apType"));
			   	project.setDelistCode(jsonObject.getString("delistCode"));
			   	project.setDelistingEntrustType(jsonObject.getString("delistingEntrustType"));
			   	project.setAddress(jsonObject.getString("address"));
			   	project.setOpticalCableFinishDate(jsonObject.getString("opticalCableFinishDate"));
			    project.setTransmissionOpenDate(jsonObject.getString("transmissionOpenDate"));
			    project.setFocus(jsonObject.getString("focus"));
			    project.setProgressInfo(jsonObject.getString("progressInfo"));
			    project.setWorkItemQuantity(jsonObject.getString("workItemQuantity"));
			    project.setSpecialtyOutput(jsonObject.getString("specialtyOutput"));
			    project.setConstructionTeam(jsonObject.getString("constructionTeam"));
			    
			    projectSearchServiceDb.saveSubProject(project);
				
			    listItem.add(map);
		    }  
			return listItem;
		} catch (JSONException e) {
			e.printStackTrace();  
			ToastShow(getString(R.string.project_search_failure));
		}
		return listItem;
		
	}
	
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			this.finish();
			finishActivitySlide();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
